#ifndef LAURIN_MODULE_H_
#define LAURIN_MODULE_H_

#include <glibmm/ustring.h>
#include <glibmm/quark.h>
#include <glibmm/thread.h>


class LaurinModule
{
	public:

		virtual ~LaurinModule();
		bool operator=(const LaurinModule& op);

		Glib::ustring getModuleName() const;

		void stop(bool wait = false);
		void start();
		bool isWorking();

		void registerOperationCompletedCallback(sigc::slot<void> callback);

	protected:

		LaurinModule(const Glib::ustring& moduleName);

		bool isStopRequestPending();
		virtual void run() = 0;
		virtual void innerStop();

	private:


		void setActive();				// defer the creation of the thread
		void wait();					// pause the thread
		void wakeup();					// wake up the sleeping thread


		Glib::Quark* _id;
		Glib::ustring _moduleName;
		Glib::Mutex _mutex;
		Glib::Cond _wait;
		Glib::Cond _stopWait;
		Glib::Thread* _thread;

		bool _wakeup;					// is thread sleeping? (different from working!)
		bool _active;					// check if thread is alive.
		bool _exit;						// for exiting purpose
		bool _working;					// is thread working?
		bool _stopRequest;				// is stop request pending?

		sigc::signal<void> _signal;
};

#endif
